﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models.Tables;

namespace WebUI.Controllers
{
    public class PartController : Controller
    {
        MainContext db = new MainContext();
        //
        // GET: /Part/

        /// <summary>
        /// 顶部导航
        /// </summary>
        /// <returns></returns>
        public ActionResult TopNav()
        {
            var list = Common.Helper.GetNavNoLevel(db.Navs.Where(m => m.FlagState == true && m.NavPosition == NavPosition.顶部).OrderBy(m => m.Sort).ToList());
            return PartialView(list);
        }

        /// <summary>
        /// 底部导航
        /// </summary>
        /// <returns></returns>
        public ActionResult BottomNav()
        {
            var list = Common.Helper.GetNavNoLevel(db.Navs.Where(m => m.FlagState == true && m.NavPosition == NavPosition.底部).OrderBy(m => m.Sort).ToList());
            return PartialView(list);
        }

        /// <summary>
        /// 主导航
        /// </summary>
        /// <returns></returns>
        public ActionResult MainNav()
        {
            var list = Common.Helper.GetNavNoLevel(db.Navs.Where(m => m.FlagState == true && m.NavPosition == NavPosition.主导航).OrderBy(m => m.Sort).ToList());
            return PartialView(list);
        }

        /// <summary>
        /// 友情链接
        /// </summary>
        /// <returns></returns>
        public ActionResult Links()
        {
            var list = db.Links.Where(m => m.FlagState == true).OrderBy(m => m.Sort).ToList();
            return PartialView(list);
        }

        /// <summary>
        /// 首页产品
        /// </summary>
        /// <returns></returns>
        public ActionResult HomeProduct()
        {
            var count = db.SystemParams.FirstOrDefault().HomeDisplayProduct;
            var list = db.Products.Where(m => m.FlagState == true).OrderBy(m => m.HomeSort).Take(count).ToList();
            return PartialView(list);
        }

        /// <summary>
        /// 首页文章
        /// </summary>
        /// <returns></returns>
        public ActionResult HomeArticle()
        {
            var count = db.SystemParams.FirstOrDefault().HomeDisplayArticle;
            var list = db.Articles.Where(m => m.FlagState == true).OrderBy(m => m.HomeSort).Take(count).ToList();
            return PartialView(list);
        }

        /// <summary>
        /// 产品分类
        /// </summary>
        /// <param name="cid">分类ID</param>
        /// <returns></returns>
        public ActionResult ProductCategory(int cid = 0)
        {
            var list = Common.Helper.GetCategoryNoLevel<ProductCategory>(db.ProductCategories.Where(m => m.FlagState == true).OrderBy(m => m.Sort).ToList());
            return PartialView(list);
        }

        /// <summary>
        /// 文章分类
        /// </summary>
        /// <param name="cid">分类ID</param>
        /// <returns></returns>
        public ActionResult ArticleCategory(int cid = 0)
        {
            var list = Common.Helper.GetCategoryNoLevel<ArticleCategory>(db.ArticleCategories.Where(m => m.FlagState == true).OrderBy(m => m.Sort).ToList());
            return PartialView(list);
        }

        /// <summary>
        /// 单页面分类
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public ActionResult SinglePageCategory(int pid = 0)
        {
            var list = Common.Helper.GetSinglePageNoLevel(db.SinglePages.Where(m => m.FlagState == true).ToList(), pid);
            return PartialView(list);
        }

        /// <summary>
        /// 商品列表(分页)
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="cid">分类ID</param>
        /// <returns></returns>
        public ActionResult ProductList(int page = 1, int cid = 0)
        {
            var size = db.SystemParams.FirstOrDefault().DisplayProduct;
            var list = db.Products.Where(m => m.FlagState == true).OrderByDescending(m => m.CreateDateTime).ToList();
            ViewBag.TotalCount = list.Count;//总数量
            ViewBag.TotalPage = Math.Ceiling((decimal)list.Count / size);//总页数
            if (cid != 0)
                list = list.Where(m => m.CatID == cid).ToList();
            list = list.Skip((page - 1) * size).Take(size).ToList();
            return PartialView(list);
        }

        /// <summary>
        /// 文章列表(分页)
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="cid">分类ID</param>
        /// <returns></returns>
        public ActionResult ArticleList(int page = 1, int cid = 0)
        {
            var size = db.SystemParams.FirstOrDefault().DisplayArticle;
            var list = db.Articles.Where(m => m.FlagState == true).OrderByDescending(m => m.CreateDateTime).ToList();
            ViewBag.TotalCount = list.Count;//总数量
            ViewBag.TotalPage = Math.Ceiling((decimal)list.Count / size);//总页数
            if (cid != 0)
                list = list.Where(m => m.CatID == cid).ToList();
            list = list.Skip((page - 1) * size).Take(size).ToList();
            return PartialView(list);
        }

        /// <summary>
        /// 产品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ProductDetail(int id)
        {
            return PartialView(db.Products.Find(id));
        }

        /// <summary>
        /// 文章详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ArticleDetail(int id)
        {
            return PartialView(db.Articles.Find(id));
        }
    }
}
